package liyouxuan;

import java.util.*;

public class DoubleColorBall {
    private static final int RED_BALL_COUNT = 6;
    private static final int BLUE_BALL_MAX = 16;
    private static final int RED_BALL_MAX = 33;

    public static void main(String[] args) {
        // 生成中奖号码
        int[] winningNumbers = generateWinningNumbers();

        // 获取用户投注号码
        int[] userNumbers = getUserNumbers();

        // 比较结果
        compareNumbers(winningNumbers, userNumbers);
    }

    // 生成中奖号码（红球不重复）
    private static int[] generateWinningNumbers() {
        int[] numbers = new int[7];
        Set<Integer> redBalls = new HashSet<>();
        Random random = new Random();

        // 生成红球
        while (redBalls.size() < RED_BALL_COUNT) {
            int num = random.nextInt(RED_BALL_MAX) + 1;
            redBalls.add(num);
        }
        Integer[] redArray = redBalls.toArray(new Integer[0]);
        for (int i = 0; i < RED_BALL_COUNT; i++) {
            numbers[i] = redArray[i];
        }

        // 生成蓝球
        numbers[6] = random.nextInt(BLUE_BALL_MAX) + 1;
        return numbers;
    }

    // 获取用户输入号码
    private static int[] getUserNumbers() {
        Scanner scanner = new Scanner(System.in);
        int[] numbers = new int[7];
        Set<Integer> redBalls = new HashSet<>();

        // 输入红球
        for (int i = 0; i < RED_BALL_COUNT; ) {
            System.out.print("请输入红球号码（1-33，剩余" + (RED_BALL_COUNT - i) + "个）：");
            int num = scanner.nextInt();
            if (num < 1 || num > RED_BALL_MAX) {
                System.out.println("号码超出范围！");
            } else if (redBalls.contains(num)) {
                System.out.println("号码重复！");
            } else {
                redBalls.add(num);
                numbers[i] = num;
                i++;
            }
        }

        // 输入蓝球
        while (true) {
            System.out.print("请输入蓝球号码（1-16）：");
            int num = scanner.nextInt();
            if (num >= 1 && num <= BLUE_BALL_MAX) {
                numbers[6] = num;
                break;
            }
            System.out.println("号码超出范围！");
        }
        return numbers;
    }

    // 比较号码并输出结果
    private static void compareNumbers(int[] winning, int[] user) {
        // 分离红球和蓝球
        int[] winRed = Arrays.copyOf(winning, 6);
        int[] userRed = Arrays.copyOf(user, 6);
        int winBlue = winning[6];
        int userBlue = user[6];

        // 统计红球命中
        int redCount = 0;
        Set<Integer> winRedSet = new HashSet<>();
        for (int num : winRed) winRedSet.add(num);
        for (int num : userRed) {
            if (winRedSet.contains(num)) redCount++;
        }

        // 统计蓝球命中
        boolean blueMatch = (winBlue == userBlue);

        // 输出信息
        System.out.println("\n中奖号码为：");
        System.out.println(Arrays.toString(winning));
        System.out.println("您投注号码为：");
        System.out.println(Arrays.toString(user));
        System.out.printf("您命中了%d个红色球号码，%d个蓝色球号码。\n",
                redCount, blueMatch ? 1 : 0);

        // 判断中奖等级
        String prize = determinePrize(redCount, blueMatch);
        System.out.println(prize);
    }

    // 判断中奖等级
    private static String determinePrize(int red, boolean blue) {
        if (red == 6 && blue) return "恭喜！一等奖（最高1000万）！";
        if (red == 6) return "恭喜！二等奖（最高500万）！";
        if (red == 5 && blue) return "恭喜！三等奖（3000元）！";
        if ((red == 5 && !blue) || (red == 4 && blue)) return "四等奖（200元）";
        if ((red == 4 && !blue) || (red == 3 && blue)) return "五等奖（10元）";
        if (blue) {
            if (red == 2 || red == 1 || red == 0) return "六等奖（5元）";
        }
        return "很遗憾，您未中奖！";
    }
}
